import requests
from jsonpath import jsonpath
from openpyxl import workbook

# 创建excel表格对象
wb = workbook.Workbook()
ws = wb.active  # 激活Excel表格
ws.append(['职位', '地点', '种类','责任','相关链接','工作年龄要求'])

def get_data(url):
    header = {
            'User-Agent':
                'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0'
        }
    result=requests.get(url,headers=header)
    data=result.json()
    return data

def prase_data(data):
    locations=jsonpath(data,'$..LocationName')
    recruits=jsonpath(data,'$..RecruitPostName')
    categorys=jsonpath(data,'$..CategoryName')
    responsibilitys=jsonpath(data,'$..Responsibility')
    urls=jsonpath(data,'$..PostURL')
    requireWorkYears=jsonpath(data,'$..RequireWorkYearsName')
    save_data(recruits,locations,categorys,responsibilitys,urls,requireWorkYears)
    # for recruit,location,category,responbility,url,requireWorkYear in zip(recruits,locations,categorys,responsibilitys,urls,requireWorkYears):
    #     print(recruit)
    #     print(location)
    #     print(category)
    #     print(responbility)
    #     print(url)
    #     print(requireWorkYear)
    #     print("====================")

def save_data(recruits,locations,categorys,responsibilitys,urls,requireWorkYears):
    for recruit, location, category, responbility, url, requireWorkYear in zip(recruits, locations, categorys,responsibilitys, urls, requireWorkYears):
        ws.append([recruit, location, category, responbility, url, requireWorkYear])
        print('职位:'+recruit)
        print('地点:'+location)
        print('责任:'+responbility)
        print('种类:'+category)
        print('相关链接:'+url)
        print('工作年龄要求:'+requireWorkYear)
        print('=====================================================')
    wb.save('腾讯招职.xlsx')

if __name__=='__main__':
    #爬取前10页信息
    for i in range(1,11):
        url = 'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1690291383277&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex={}&pageSize=10&language=zh-cn&area=cn'.format(i)
        data=get_data(url)
        prase_data(data)

